package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    public R save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    @PutMapping("/default")
    public R setdefault(@RequestBody AddressBook addressBook){
        log.info("addressBook:{}",addressBook);

        LambdaUpdateWrapper<AddressBook> qwaddress = new LambdaUpdateWrapper<>();
        qwaddress.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        qwaddress.set(AddressBook::getIsDefault,0);
        addressBookService.update(qwaddress);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    @GetMapping("/default")
    public R getdefault(){

        LambdaQueryWrapper<AddressBook> qwaddress = new LambdaQueryWrapper<>();
        qwaddress.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        qwaddress.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(qwaddress);
        if(addressBook==null){
            return R.error("没有该对象");
        }
        return R.success(addressBook);
    }

    @GetMapping("/{id}")
    public R get(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if(addressBook!=null){
            return R.success(addressBook);
        }
        return R.error("查询失败");
    }

    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("address:{}",addressBook);
        LambdaQueryWrapper<AddressBook> qwsddress = new LambdaQueryWrapper<>();
        qwsddress.eq(addressBook.getUserId()!=null,
                AddressBook::getUserId,addressBook.getUserId());

        qwsddress.orderByDesc(AddressBook::getUpdateTime);
        //添加排序条件
        List<AddressBook> bookList = addressBookService.list(qwsddress);
        return R.success(bookList);

    }


    @DeleteMapping
    public R deleteDish(@RequestParam List<Long> ids){
        System.out.println(ids);
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }


    @PutMapping
    public R update(@RequestBody AddressBook addressBook){
        log.info(addressBook.toString());

        addressBookService.updateById(addressBook);
        return R.success("修改地址成功");
    }


}
